package com.piik.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.piik.mall.product.entity.BrandEntity;
import com.piik.mall.product.entity.CategoryEntity;
import com.piik.mall.product.service.BrandService;
import com.piik.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piik.common.utils.PageUtils;
import com.piik.common.utils.Query;

import com.piik.mall.product.dao.CategoryBrandRelationDao;
import com.piik.mall.product.entity.CategoryBrandRelationEntity;
import com.piik.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> categoryBrandRelation(Long catId) {

        // 找到所有品牌信息
        List<CategoryBrandRelationEntity> list = categoryBrandRelationDao
                .selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
        return list;
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 1. 根据品牌编号查询品牌名称
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        if(brandEntity != null){
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }

        // 2. 根据分类编号查询分类名称
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        if(categoryEntity != null){
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        // 3. 查看品牌与分类是否已关联
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",categoryBrandRelation.getBrandId());
        wrapper.eq("catelog_id",categoryBrandRelation.getCatelogId());
        Integer count  = categoryBrandRelationDao.selectCount(wrapper);
        if(count>0){
            // 3.1 已关联更新
            this.updateById(categoryBrandRelation);
        }else {
            // 3.2 没有新增关联
            this.save(categoryBrandRelation);
        }

    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        this.update(entity
                ,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCatelogName(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogName(name);
        entity.setCatelogId(catId);
        this.update(entity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }

}